package com.ss.android.article.base.feature.subscribe.b;

import android.content.Context;
import android.os.Looper;

import com.bytedance.common.utility.Logger;
import com.ss.android.account.Spipe;
import com.ss.android.account.b.OnAccountRefreshListener;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.subscribe.model.SubscribeData;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class SubscribeManager {
    public interface SubscribeDataChangeListener {
        void onSubscribeDataChanged(SubscribeData arg1);
    }

    public Context mContext;
    Spipe mSpipe;
    public com.ss.android.article.base.feature.subscribe.b.e c;
    boolean isLogin;
    private static SubscribeManager instance;
    private OnAccountRefreshListener mOnAccountRefreshListener;

    private SubscribeManager(Context context) {
        this.mContext = context.getApplicationContext();
        this.mSpipe = Spipe.inst();
        this.c = new com.ss.android.article.base.feature.subscribe.b.e(this.mContext);
        this.isLogin = this.mSpipe.isLogin;
        if (Logger.debug()) {
            Logger.d("SubscribeManager", "initial login status: " + this.mSpipe.isLogin);
        }

        if (this.mOnAccountRefreshListener == null) {
            this.mOnAccountRefreshListener = new com.ss.android.article.base.feature.subscribe.b.d(this);
            this.mSpipe.addOnAccountRefreshListener(this.mOnAccountRefreshListener);
        }
    }

    public static void init(Context context) {
        synchronized (SubscribeManager.class) {
            if (context != null) {
                if (instance == null) {
                    instance = new SubscribeManager(context);
                }
            }
        }
    }

    public final void addSubscribeDataChangeListener(SubscribeDataChangeListener subscribeDataChangeListener) {
        checkUIThread();
        com.ss.android.article.base.feature.subscribe.b.e v0 = this.c;
        if (subscribeDataChangeListener != null) {
            v0.subscribeDataChangeListenerList.add(subscribeDataChangeListener);
        }
    }

    public static synchronized SubscribeManager getInstance() {
        try {
            if (instance != null) {
                return instance;

            }
            init(AppData.getInstance().getAppContext().getContext());
            if (instance == null) {
                throw new IllegalStateException("SubscribeManager has not initialized.");
            }
        } catch (Throwable throwable) {

        }

        return instance;
    }

    public final boolean a(long arg6, boolean arg8) {
        Object v0_1;
        ArrayList v0 = new ArrayList();
        this.a(((List) v0));
        Iterator v1 = v0.iterator();
        do {
            if (v1.hasNext()) {
                v0_1 = v1.next();
                if (((com.ss.android.article.base.feature.subscribe.model.d) v0_1).a == null) {
                    continue;
                }

                if (((com.ss.android.article.base.feature.subscribe.model.d) v0_1).b != arg6) {
                    continue;
                }

                break;
            } else {
                return DBHelper.getInstance(this.mContext).a(arg6, arg8);
            }
        }
        while (true);

        boolean v0_2 = ((com.ss.android.article.base.feature.subscribe.model.d) v0_1).a.isSubscribed();
        return v0_2;

    }

    public final void a(List arg2) {
        checkUIThread();
        com.ss.android.article.base.feature.subscribe.b.e v0 = this.c;
        arg2.clear();
        arg2.addAll(v0.i);
    }

    public final void b(SubscribeDataChangeListener arg2) {
        checkUIThread();
        com.ss.android.article.base.feature.subscribe.b.e v0 = this.c;
        if (arg2 != null) {
            v0.subscribeDataChangeListenerList.remove(arg2);
        }
    }

    public final void b() {
        checkUIThread();
        if (!this.c.d) {
            this.c.a(1);
        }
    }

    public final void c() {
        this.c.a(e());
    }

    public static void checkUIThread() {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            throw new IllegalStateException("All methods of SubscribeManager must be called in UI Thread");
        }
    }

    public static long e() {
        long v0 = 60;
        AppData.getInstance();
        long v2 = AppData.I();
        if (-1 < 0) {
            v2 = 1800;
        }
        if (v2 >= v0) {
            v0 = v2;
        }
        return v0 * 1000;
    }
}

